package cn.zshprint.serein.controller;

import cn.zshprint.serein.entity.User;
import cn.zshprint.serein.pojo.ResultBean;
import cn.zshprint.serein.service.UserService;
import cn.zshprint.serein.vo.LoginVO;
import cn.zshprint.serein.vo.UserRegisterVO;
import cn.zshprint.serein.vo.UserReturnVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

/**
 * 用户控制器
 * (User)表控制层
 *
 * @author makejava
 * @date 2021/07/28
 * @since 2021-07-28 11:04:32
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 用户服务
     */
    @Resource
    private UserService userService;

    /**
     * 注册
     *
     * @param userRegisterVO 用户注册签证官
     * @return {@link ResultBean<Integer>}
     */
    @PostMapping("/register")
    public ResultBean<Integer> register(@RequestBody UserRegisterVO userRegisterVO, @RequestParam(value = "type",required = false) String type) throws NoSuchAlgorithmException, InvalidKeySpecException {
        return new ResultBean<>(userService.register(userRegisterVO,type));
    }


    /**
     * 登录
     *
     * @param loginVO 登录签证官
     * @return {@link ResultBean<User>}
     */
    @PostMapping("/login")
    public ResultBean<UserReturnVO> login(@RequestBody LoginVO loginVO) throws NoSuchAlgorithmException, InvalidKeySpecException {
        return new ResultBean<>(userService.login(loginVO));
    }


    /**
     * 找到一个通过id
     *
     * @param id id
     * @return {@link ResultBean<UserReturnVO>}
     */
    @GetMapping("/getOneById/{id}")
    public ResultBean<UserReturnVO> findOneById(@PathVariable Integer id) {
        return new ResultBean<>(userService.getOneById(id));
    }

    /**
     * 找到的用户名
     *
     * @param username 用户名
     * @return {@link ResultBean<User>}
     */
    @GetMapping("/getOneByUsername/{username}")
    public ResultBean<UserReturnVO> findOneByUsername(@PathVariable String username) {
        return new ResultBean<>(userService.getOneByUsername(username));
    }


    @GetMapping("/findAll")
    public ResultBean<List<UserReturnVO>> findAll() {
        return new ResultBean<>(userService.findAll());
    }

    /**
     * 更新用户
     *
     * @param user 用户
     * @return {@link ResultBean<User>}
     */
    @PutMapping("/updateUser")
    public ResultBean<User> updateUser(@RequestBody User user) {
        return new ResultBean<>(userService.updateUser(user));
    }

    /**
     * 删除用户
     *
     * @param id id
     * @return {@link ResultBean<Integer>}
     */
    @DeleteMapping("/deleteUser/{id}")
    public ResultBean<Integer> deleteUser(@PathVariable Integer id) {
        return new ResultBean<>(userService.deleteUser(id));
    }
}